package com.doctor.windflower_doctor.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.activity.ReportActivity;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.QuestionBeen;
import com.doctor.windflower_doctor.fragment.MyQuestionHallFragment;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.RefreshIntent;
import com.doctor.windflower_doctor.http.parser.RefreshMiss;
import com.doctor.windflower_doctor.http.parser.RefreshMyShow;
import com.doctor.windflower_doctor.http.parser.RefreshShow;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.RoundedImageView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class MyQuestionAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater inflater;
	private List<QuestionBeen> list;
	private int type = 0;//定时状态
	private DisplayImageOptions options;
	private String a, b, c;
	public int time, num;
	private RefreshShow refreshShow;
	private RefreshIntent refreshIntent;
	private RefreshMyShow refreshMyShow;
	private RefreshMiss refreshMiss;
	private boolean isAccept;

	public MyQuestionAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.list = new ArrayList<QuestionBeen>();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.loading_img)
				.showImageOnFail(R.drawable.loading_img)
				.showImageForEmptyUri(R.drawable.loading_img)
				.cacheInMemory(true).cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.ARGB_8888).build();
	}

	public void setData(List<QuestionBeen> list) {
		this.list = list;
	}

	public void addData(List<QuestionBeen> data, int time, boolean isAccept) {
		if (data != null) {
			this.list.addAll(data);
		}
		this.time = time;
		this.isAccept = isAccept;
		notifyDataSetChanged();
	}

	public void clear() {
		if (list != null) {
			list.clear();
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public void setText(String a, String b) {
		this.a = a;
		this.b = b;
		notifyDataSetChanged();
	}

	private void postTime(String questionId) {
		String url = Constent.URL + "ask/doctor/answerQuestion";
		HttpMethod method = HttpMethod.POST;
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(
				new JsonParser(), QuestionAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<QuestionAction> responseInfo) {
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {

						refreshIntent.isRefresh(true);

					} else {
						if (SharedPreferencesDB.getInstance(context)
								.getIsAcceptQuestion()) {
							Toast.makeText(context,
									responseInfo.success.message, 1).show();
						} else {
							if (!isAccept)
								refreshMyShow.isRefreshs(true);
							else
								refreshShow.isRefreshs(true);
						}
					}
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub

			}
		};
		HttpRequestParams params = new HttpRequestParams(method, url,
				requestCallBack);
		params.addBodyParameter("doctorId",
				SharedPreferencesDB.getInstance(context).getDocotorId());
		params.addBodyParameter("questionId", questionId);
		HttpRequest.send(params);
	}

	public void setShow(RefreshShow refreshShow) {
		this.refreshShow = refreshShow;
	}

	public void setMiss(RefreshMiss refreshMiss) {
		this.refreshMiss = refreshMiss;
	}

	public void setIntent(RefreshIntent refreshIntent) {
		this.refreshIntent = refreshIntent;
	}

	public void setMyShow(RefreshMyShow refreshMyShow) {
		this.refreshMyShow = refreshMyShow;
	}

	@Override
	public View getView(final int arg0, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHoldera holder;
		if (convertView == null) {
			holder = new ViewHoldera();
			convertView = inflater.inflate(R.layout.questionhall_item, arg2,
					false);
			holder.iv_user = (RoundedImageView) convertView
					.findViewById(R.id.imageView1);
			holder.tv_nick = (TextView) convertView.findViewById(R.id.tv_nick);
			holder.tv_age = (TextView) convertView.findViewById(R.id.tv_age);
			holder.tv_rob = (TextView) convertView.findViewById(R.id.tv_rob);
			holder.hour = (TextView) convertView.findViewById(R.id.hour);
			holder.minute = (TextView) convertView.findViewById(R.id.minute);
			holder.tv_pre = (TextView) convertView.findViewById(R.id.tv_pre);
			holder.item_layout = (RelativeLayout) convertView
					.findViewById(R.id.item_layout);
			holder.layout = (RelativeLayout) convertView
					.findViewById(R.id.relativeLayout2);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
			holder.tv_content = (TextView) convertView
					.findViewById(R.id.textView1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHoldera) convertView.getTag();
		}
		if (time == 0) {
			type = 0;
			
		} else {

			type = 1;
		}
		if (type == 0) {
			holder.tv_rob.setVisibility(View.VISIBLE);
			holder.layout.setVisibility(View.GONE);

		} else {
			holder.tv_rob.setVisibility(View.GONE);
			holder.layout.setVisibility(View.VISIBLE);
			holder.tv_rob.setVisibility(View.GONE);
			holder.layout.setVisibility(View.VISIBLE);

			if (a != null) {
				if (Integer.valueOf(a) < 10) {
					c = "0" + a;
				} else {
					c = a;
				}
			}
			holder.minute.setText(c);
			holder.hour.setText(b);
			if (a != null) {
				if (a.equals("0")
						&& holder.hour.getText().toString().equals("0")) {
					holder.tv_rob.setVisibility(View.VISIBLE);
					holder.layout.setVisibility(View.GONE);
					type=0;
					notifyDataSetChanged();
				}
			}
		}

		SystemApplication.getInstance().mImageLoad1.displayImage(
				list.get(arg0).imgUrl, holder.iv_user, options);
		holder.tv_age.setText(list.get(arg0).age + "岁");
		holder.tv_content.setText(list.get(arg0).text);
		holder.tv_nick.setText(list.get(arg0).nick);
		holder.tv_time.setText(list.get(arg0).createTime);
		holder.tv_type.setText(list.get(arg0).classification);
		holder.tv_pre.setText(list.get(arg0).prePeriod);
		holder.item_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				refreshMiss.isMiss(true);
				if(type==0){
				Intent intent = new Intent(context, ReportActivity.class);
				intent.putExtra("itme", list.get(arg0));
				context.startActivity(intent);}
			}
		});
		holder.tv_rob.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View argw) {

				// TODO Auto-generated method stub
				// type=1;
				postTime(list.get(arg0).questionId);
				notifyDataSetChanged();
			}
		});

		holder.layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "请在倒计时后再次抢答", 1).show();
			}
		});

		
		
		return convertView;
	}



}

class ViewHoldera {
	RoundedImageView iv_user;
	TextView tv_nick;
	TextView tv_age;
	TextView tv_content;
	TextView hour;
	TextView minute;
	TextView tv_type;
	TextView tv_time;
	TextView tv_rob;
	TextView tv_pre;
	RelativeLayout item_layout;
	RelativeLayout layout;
}
